package com.jjwxc.jwjskandriod.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.base.FFApplication;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.model.ListBookGroupResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class GroupPop {

    /**
     * 编辑弹出框
     */
    static boolean mRefresh = false;
    private static List<ListBookGroupResponse.DataBean> editGlist = new ArrayList<>();

    public static void editGroupPop(View v, Activity context, PopDismissListener popDismissListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.edit_group_pop, null, false);
        LinearLayout linearLayout = view.findViewById(R.id.ll_views);
        TextView tv_xinjian = view.findViewById(R.id.tv_xinjian);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        tv_quxiao.setOnClickListener(v12 -> {
            popWindow.dismiss();
        });

        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1, context);
                popDismissListener.onOkClick(mRefresh);
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        setBackgroundAlpha(0.3f, context);

        tv_xinjian.setOnClickListener(v13 -> {
                    if (editGlist.size() > 8) {
                        FFApplication.showToast("收藏文件夹过多,请先清理");
                        return;
                    }
                    addGroupPop(v, context, null, new PopDismissListener() {
                        @Override
                        public void onOkClick(boolean group) {
                            mRefresh = group;
                            if (group) {
                                listBookcaseGroup(v, context, linearLayout);
                            }
                        }
                    });
                }
        );
        //加载数据
        listBookcaseGroup(v, context, linearLayout);
    }

    public static void listBookcaseGroup(View view, Activity context, LinearLayout linearLayout) {
        linearLayout.removeAllViews();
        ((FFActivity) context).post(Url.listBookcaseGroup, null, ListBookGroupResponse.class, new FFNetWorkCallBack<ListBookGroupResponse>() {
            @Override
            public void onSuccess(ListBookGroupResponse response) {
                editGlist.clear();
                editGlist.addAll(response.getData());
                for (int i = 0; i < editGlist.size(); i++) {
                    View viewone = LayoutInflater.from(context).inflate(R.layout.edit_group_pop_item, null);
                    ImageView img_more = viewone.findViewById(R.id.img_more);
                    TextView tv_numbook = viewone.findViewById(R.id.tv_numbook);
                    TextView tv_title = viewone.findViewById(R.id.tv_title);
                    TextView tv_line = viewone.findViewById(R.id.tv_line);
                    if (editGlist.size() == 1) {
                        tv_line.setVisibility(View.GONE);
                    } else {
                        tv_line.setVisibility(View.VISIBLE);
                    }
                    tv_title.setText(editGlist.get(i).getName());
//                    if (i != 0) {
                    img_more.setVisibility(View.VISIBLE);
                    tv_numbook.setVisibility(View.VISIBLE);
                    tv_numbook.setText(editGlist.get(i).getBookCount() + "本");
//                    }
                    int finalI = i;
                    img_more.setOnClickListener(v1 -> {
                        updataGroupPop(view, context, editGlist.get(finalI), new PopDismissListener() {
                            @Override
                            public void onOkClick(boolean group) {
                                if (group) {
                                    //刷新数据
                                    listBookcaseGroup(view, context, linearLayout);
                                }

                            }
                        });
                    });
                    linearLayout.addView(viewone);
                }
            }

            @Override
            public boolean onFail(ListBookGroupResponse response) {
                return false;
            }
        });
    }

    /**
     * 更改
     *
     * @param v
     * @param context
     * @param dataBean
     * @param popDismissListener
     */
    public static void updataGroupPop(View v, Activity context, ListBookGroupResponse.DataBean dataBean, PopDismissListener popDismissListener) {
        final boolean[] isRefresh = {false};
        View view = LayoutInflater.from(context).inflate(R.layout.updata_group_pop, null, false);
        TextView tv_chongming = view.findViewById(R.id.tv_chongming);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        TextView tv_del = view.findViewById(R.id.tv_del);
        View view_quxiao = view.findViewById(R.id.view_quxiao);
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.ffProg_anim);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(
                () -> popDismissListener.onOkClick(isRefresh[0]));
        tv_quxiao.setOnClickListener(v1 -> {
            popWindow.dismiss();
        });
        view_quxiao.setOnClickListener(v1 -> {
            popWindow.dismiss();
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);


        tv_chongming.setOnClickListener(v13 -> {
            addGroupPop(v, context, dataBean, new PopDismissListener() {
                @Override
                public void onOkClick(boolean refresh) {
                    isRefresh[0] = refresh;
                }
            });
        });
        tv_del.setOnClickListener(v12 -> ((FFActivity) context).post(Url.removeBookcaseGroup, null, String.class, new FFNetWorkCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                isRefresh[0] = true;
                popWindow.dismiss();
            }

            @Override
            public boolean onFail(String response) {
                return false;
            }
        }, "{\"bookcaseGroupId\":\"" + dataBean.getBookcaseGroupId() + "\"}"));

    }

    /**
     * 新增
     *
     * @param v
     * @param context
     * @param dataBean
     * @param popDismissListener
     */
    public static void addGroupPop(View v, Activity context, ListBookGroupResponse.DataBean dataBean, PopDismissListener popDismissListener) {

        AtomicBoolean isRefresh = new AtomicBoolean(false);
        View view = LayoutInflater.from(context).inflate(R.layout.add_group_pop, null, false);
        EditText editText = view.findViewById(R.id.et_views);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        TextView tv_queding = view.findViewById(R.id.tv_queding);
        View view_quxiao = view.findViewById(R.id.view_quxiao);

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.ffProg_anim);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popDismissListener.onOkClick(isRefresh.get());
            }
        });
        tv_quxiao.setOnClickListener(v1 -> {
            isRefresh.set(false);
            popWindow.dismiss();
        });
        view_quxiao.setOnClickListener(v1 -> {
            isRefresh.set(false);
            popWindow.dismiss();
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        final String[] url = new String[1];
        final String[] requst = new String[1];
        if (dataBean == null) {
            url[0] = Url.addBookcaseGroup;
            requst[0] = "{\"name\":\"" + editText.getText().toString() + "\"}";
        } else {
            url[0] = Url.editBookcaseGroup;
            requst[0] = "{\"bookcaseGroupId\":\"" + dataBean.getBookcaseGroupId() + "\",\"name\":\"" + dataBean.getName() + "\"}";
        }
        tv_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean == null) {
                    requst[0] = "{\"name\":\"" + editText.getText().toString() + "\"}";
                } else {
                    requst[0] = "{\"bookcaseGroupId\":\"" + dataBean.getBookcaseGroupId() + "\",\"name\":\"" + dataBean.getName() + "\"}";
                }
                ((FFActivity) context).post(url[0], null, String.class, new FFNetWorkCallBack<String>() {
                    @Override
                    public void onSuccess(String response) {
                        isRefresh.set(true);
                        popWindow.dismiss();
                    }

                    @Override
                    public boolean onFail(String response) {
                        return false;
                    }
                }, requst[0]);
            }
        });

    }

    /**
     * pop 收藏的选择
     */
    private static String mBookcaseGroupId = null;

    public static void collentGroupPop(View mView, Activity context, PopSelectListener popSelectListener) {
        mBookcaseGroupId = null;

        View view = LayoutInflater.from(context).inflate(R.layout.edit_group_pop, null, false);

        LinearLayout linearLayout = view.findViewById(R.id.ll_views);
        TextView tv_xinjian = view.findViewById(R.id.tv_xinjian);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        tv_quxiao.setOnClickListener(v12 -> {
            popWindow.dismiss();
        });

        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1, context);
                popSelectListener.onGroupIdClick(mBookcaseGroupId);
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);
        setBackgroundAlpha(0.3f, context);
        tv_xinjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editGlist.size() > 8) {
                    FFApplication.showToast("收藏文件夹过多,请先清理");
                    return;
                }
                addGroupPop(mView, context, null, new PopDismissListener() {
                    @Override
                    public void onOkClick(boolean group) {
                        if (group) {
                            getListBookcaseGroup(context, linearLayout, popWindow);
                        }
                    }
                });
            }
        });
        //加载数据
        getListBookcaseGroup(context, linearLayout, popWindow);
    }

    public static void getListBookcaseGroup(Context context, LinearLayout linearLayout, PopupWindow popWindow) {
        linearLayout.removeAllViews();
        ((FFActivity) context).post(Url.listBookcaseGroup, null, ListBookGroupResponse.class, new FFNetWorkCallBack<ListBookGroupResponse>() {
            @Override
            public void onSuccess(ListBookGroupResponse response) {
                List<ListBookGroupResponse.DataBean> list = new ArrayList<>();
                list.addAll(response.getData());
                for (int i = 0; i < list.size(); i++) {
                    View viewone = LayoutInflater.from(context).inflate(R.layout.edit_group_pop_item, null);
                    ImageView img_more = viewone.findViewById(R.id.img_more);
                    TextView tv_numbook = viewone.findViewById(R.id.tv_numbook);
                    TextView tv_title = viewone.findViewById(R.id.tv_title);
                    TextView tv_line = viewone.findViewById(R.id.tv_line);
                    if (list.size() == 1) {
                        tv_line.setVisibility(View.GONE);
                    } else {
                        tv_line.setVisibility(View.VISIBLE);
                    }
                    tv_title.setText(list.get(i).getName());
                    img_more.setVisibility(View.VISIBLE);
                    tv_numbook.setVisibility(View.VISIBLE);
                    tv_numbook.setText(list.get(i).getBookCount() + "本");

                    int finalI = i;
                    tv_title.setOnClickListener(v14 -> {
                        mBookcaseGroupId = list.get(finalI).getBookcaseGroupId();
                        popWindow.dismiss();
                    });
                    linearLayout.addView(viewone);
                }


            }

            @Override
            public boolean onFail(ListBookGroupResponse response) {
                return false;
            }
        });
    }

    public interface PopDismissListener {
        void onOkClick(boolean isRefresh);
    }

    public interface PopSelectListener {
        void onGroupIdClick(String groupId);
    }

    //设置屏幕背景透明效果
    public static void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }
}
